package com.loong.web.suppert.intercept.statistic;

import java.util.concurrent.ExecutorService;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.method.HandlerMethod;

/**
 * 访问统计器
 *
 * @author 张成轩
 */
public class StatisticController {

	/** 线程池 */
	private ExecutorService threadPool;
	/** 统计记录器 */
	private StatisticLogger statisticLogger;

	/**
	 * @param request 请求
	 * @param handler 处理方法
	 */
	public void control(HttpServletRequest request, HandlerMethod handler) {

		if (statisticLogger == null || threadPool == null)
			return;
		Statistic s = handler.getMethodAnnotation(Statistic.class);
		if (s == null)
			return;
		// 封装统计对象
		final StatisticVo statistic = new StatisticVo(s, request, handler);
		// 启动线程
		threadPool.execute(new Runnable() {

			@Override
			public void run() {

				statisticLogger.save(statistic);
			}
		});
	}

	/**
	 * @param threadPool 线程池
	 */
	public void setThreadPool(ExecutorService threadPool) {

		this.threadPool = threadPool;
	}

	/**
	 * @param statisticLogger 统计记录器
	 */
	public void setStatisticLogger(StatisticLogger statisticLogger) {

		this.statisticLogger = statisticLogger;
	}
}
